package menu.lt.pers.menu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import menu.lt.pers.menu.Bean.ResultByClassIdBean;
import menu.lt.pers.menu.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ProcessRecyclerViewAdapter extends RecyclerView.Adapter<ProcessRecyclerViewAdapter.MyHolder>{
    private Context context;
    private List<ResultByClassIdBean.ResultBean.ListBean.ProcessBean> list;
    private HashMap<String,Bitmap> bitmapList;

    public ProcessRecyclerViewAdapter(Context context,
                                      List<ResultByClassIdBean.ResultBean.ListBean.ProcessBean> list) {
        this.context = context;
        this.list = list;
        this.bitmapList = new HashMap<>();
    }

    public Context getContext() {
        return context;
    }
    public void update(HashMap<String,Bitmap> bitmapList){
        this.bitmapList = bitmapList;
        notifyDataSetChanged();
    }

    public List<ResultByClassIdBean.ResultBean.ListBean.ProcessBean> getList() {
        return list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.activity_recipe_info_listview_process_resource,viewGroup,false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {

        new GetBimap().execute(list.get(i).getPic(),list.get(i).getPcontent());
        if (bitmapList.get(list.get(i).getPcontent())!=null){
            myHolder.imageview_pic.setImageBitmap(bitmapList.get(list.get(i).getPcontent()));
        } else {
            myHolder.imageview_pic.setImageResource(R.drawable.timg);
        }
        String[] str = list.get(i).getPcontent().split("<br />");
        myHolder.tv_pccontent.setText(i+1+". "+str[0]);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_pccontent;
        ImageView imageview_pic;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_pccontent = itemView.findViewById(R.id.tv_pccontent);
            imageview_pic = itemView.findViewById(R.id.imageview_pic);
        }
    }
    class GetBimap extends AsyncTask {
        private String key;
        @Override
        protected Bitmap doInBackground(Object[] params) {
            key = (String) params[1];
            if (bitmapList.get(key)==null){
                String uripath = (String) params[0];
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(5000, TimeUnit.SECONDS)
                        .readTimeout(5000,TimeUnit.SECONDS)
                        .build();
                Request request = new Request.Builder()
                        .url(uripath)
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()){
                        InputStream inputStream = response.body().byteStream();//得到图片的流
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        return bitmap;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (o!=null){
                Bitmap bitmap = (Bitmap) o;
                bitmapList.put(key,bitmap);
                    bitmapList.put(key,bitmap);
                    update(bitmapList);
                }
            }
        }

}
